package cj.web.admin.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.admin.dao.AreaDAO;
import cj.web.admin.dao.SchoolDAO;
import cj.web.admin.domain.Area;
import cj.web.admin.domain.School;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/school")
public class SchoolController extends BaseController{

	private static Logger logger = Logger.getLogger(SchoolController.class.getName());
	
	@Autowired
	private SchoolDAO schoolDao;
	@Autowired
	private AreaDAO areaDao;
	
	@Get("show")
	public String show(Model model,School school) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.xxgl);
		return "/views/game/schoolList.vm";
	}

	@Post("getList")
	public String queryList(School school) {
	
		JSONObject jo = new JSONObject();
		Map rep = new HashMap();
		List<Area> areaObjs = areaDao.queryAll();
		for(Area obj:areaObjs){
			rep.put(obj.getId(), obj.getCode()+":"+obj.getName());
		}
		
		List<School> objs = schoolDao.query(school);
		for(School obj:objs){
			String str = rep.get(obj.getAreaId()).toString();
			obj.setAreaCode(str.split(":")[0]);
			obj.setAreaName(str.split(":")[1]);
		}
		
		int size = schoolDao.querysize(school);
		jo.put("data", objs);
		jo.put("draw", school.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		List<Area> areaObjs = areaDao.queryAll();
		model.add("areaObjs",areaObjs);
		//MemuEnum
        model.add("MemuEnum",MemuEnum.xxgl);
		return "/views/game/schoolDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,School school,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			school.setCreateUser(returnSessionObj(request).getAccount());
			school.setUpdateUser(returnSessionObj(request).getAccount());
			schoolDao.create(school);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		School obj = schoolDao.queryById(id);
		model.add("obj", obj);
		
		List<Area> areaObjs = areaDao.queryAll();
		model.add("areaObjs",areaObjs);
		//MemuEnum
        model.add("MemuEnum",MemuEnum.xxgl);
		return "/views/game/schoolEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,School school,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			school.setUpdateUser(returnSessionObj(request).getAccount());
			schoolDao.update(school);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		schoolDao.deleteById(id);
		return "r:show";
	}
}
